package com.starbaba.system.attach.dao;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.HibernateDAO;
import com.starbaba.core.hibernate.Strategy;
import com.starbaba.system.attach.model.AttachVO;
import com.starbaba.system.user.model.UserVO;
//import com.mtech.fdfs.FdfsManager;
import com.tuanzi.server.loan_base.util.DateFormatFactory;

/**
 * 上传文件，并创建附件记录
 *
 * @Copyright: Copyright (c) 2006
 * @Company: Revenco Tech Ltd.
 * @anthor chenminhui<chenminhui@revenco.com>
 * @since 2014-11-5 上午10:21:58
 * @version V1.0
 */
@SuppressWarnings("serial")
public class UploadAttachStrategy implements Strategy {
	
	Logger log = LoggerFactory.getLogger(getClass());
	
	MultiPartRequestWrapper request;
	String fieldName;
	String servicecode;
	UserVO user;
	
	public UploadAttachStrategy(MultiPartRequestWrapper request, String fieldName, String servicecode, UserVO user) {
		this.request = request;
		this.fieldName = fieldName;
		this.servicecode = servicecode;
		this.user = user;
	}


	/* (non-Javadoc)
	 * @see com.starbaba.core.hibernate.Strategy#process()
	 */
	@Override
	public List<AttachVO> process() throws Exception {
		Date now = new Date();
		String month = DateFormatFactory.getInstance("yyyyMM").format(now);
			
		
		String fileNames[] = request.getFileNames(fieldName); //上传的原文件名
		File files[] = request.getFiles(fieldName);	  //对应的系统临时文件
		String contentTypes[] = request.getContentTypes(fieldName); //文件类型;zip:application/x-zip-compressed
//		String fileSystemNames[] = request.getFileSystemNames(fileInputId); //对应的系统临时文件名,是files的没有目录的文件名
		
		List<AttachVO> rtnList = new ArrayList<AttachVO>();
		if(ArrayUtils.isNotEmpty(files)) {
			HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO(AttachVO.class);
			
			for(int i = 0; i < files.length; i++) {
				File file = files[i];
				String oldfilename = fileNames[i];
			 	//2. 服务端临时文件上传到FastDFS
//				List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
//				nvpList.add(new NameValuePair("filename", oldfilename));
//				nvpList.add(new NameValuePair("filesize", String.valueOf(file.length())));
//				nvpList.add(new NameValuePair("contenttype", contentTypes[i]));
				
				log.info("准备上传文件{}(本地：{})到FastDFS...", oldfilename, file.getCanonicalPath());
//				String savePath = FdfsManager.uploadFile(file, nvpList.toArray(new NameValuePair[0]));
				log.info("文件{}(远程：{})成功上传FastDFS", oldfilename, file.getCanonicalPath());
				
				//4. 记录到库表【附件表】
				AttachVO attachVO = new AttachVO();
				attachVO.setFilename(oldfilename);
//				attachVO.setSavepath(savePath);
				attachVO.setSavemonth(month);
				attachVO.setUploadtime(now);
				attachVO.setServicecode(servicecode);
				attachVO.setComid(user.getComid());
				attachVO.setUserid(user.getUserid());
				attachVO.setContenttype(contentTypes[i]);
				
				dao.save(attachVO);
				
				rtnList.add(attachVO);
			}
		}
		
		return rtnList;
	}

}
